package com.czy.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @Author: czy
 * @Date: 2019/11/8 15:47
 */
@Service
public class EmailUtil {
    @Value("${spring.mail.username}")
    private String sender;

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送注册激活邮件
     * @param email
     * @param activiteCode
     */
    public void sendHtmlMail(String email,String activiteCode){
        MimeMessage mimeMessage = null;
        try {
            mimeMessage=mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

            helper.setFrom(sender);
            helper.setTo(email);
            helper.setSubject("主题:wechat激活邮件");
            String text="<html>" +
                    "<body>" +
                    "<h3>微信客户端激活邮件</h3>" +
                    "<a href='http://122.51.49.32:80/account/activited?activiteCode="+activiteCode+"'>点击激活</a>"+
                    "</body>" +
                    "</html>";
            helper.setText(text,true);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        mailSender.send(mimeMessage);
    }
}
